package com.yanqu.road.server.cmd.newmonopolymkt.res;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.newmonopolymarket.NewMonopolyMarketUserPhysicalChangeLogData;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.resgrab.NewMonopolyMarketTempPoint;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.newmonopolymkt.NewMonopolyMarketModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.systemgroup.newmonopolymarket.NewMonopolyMarketMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.property.Property;


@Cmd(code = GameProtocol.S_NEW_MONOPOLY_MARKET_RES_GRAB_ACTION_FROM_CROSS, desc = "资源争夺战撤销/抢夺 位置 跨服返回区服 协议")
public class NewMonopolyMarketResGrabActionFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        NewMonopolyMarketProto.NewMonopolyMarketResGrabActionToGameServerMsg resp = NewMonopolyMarketProto.NewMonopolyMarketResGrabActionToGameServerMsg.parseFrom(packet.getBytes());

        NewMonopolyMarketModule marketModule = player.getModule(NewMonopolyMarketModule.class);

        if(resp.getStcMsg().getRet() == 0){
            NewMonopolyMarketProto.NewMonopolyMarketResGrabActionRespMsg stcMsg = resp.getStcMsg();
            if(resp.getActionType() == 0 || resp.getActionType() == 2){
                int oldValue = marketModule.userData.getGuanYin();
                if(resp.getStcMsg().getIsSuccess()){
                    // 成功抢夺
                    if(resp.hasWait()){
                        //队伍达上限
                        NewMonopolyMarketTempPoint tempPoint = new NewMonopolyMarketTempPoint();
                        tempPoint.setResGroupId(resp.getWait().getResGroupId());
                        tempPoint.setPosIndex(resp.getWait().getPosIndex());
                        tempPoint.setTime(resp.getWait().getTimeout());
                        marketModule.userData.setWaitDispatchPoint(tempPoint);
                    }else {
                        marketModule.addDispatchInfo(resp.getResGroupId(), resp.getType(), resp.getPosIndex(), stcMsg.getObjectListList(), stcMsg.getPointInfo().getStartTime());
                    }

                }else {
                    // 抢夺失败
                }
                if(resp.getActionType() == 0){
                    //清除搜索结果
                    marketModule.userData.getSearchPointMap().remove(resp.getResGroupId());
                    marketModule.userData.setUpdateOption();

                }else if(resp.getActionType() == 2) {

                    //如果抢夺资源点在搜索级果
                    if(resp.getStcMsg().getIsSuccess()){
                        NewMonopolyMarketTempPoint tempPoint = marketModule.userData.getSearchPointMap().get(resp.getResGroupId());
                        if(tempPoint != null && tempPoint.getPosIndex() == resp.getPosIndex()){
                            //清除搜索结果
                            marketModule.userData.getSearchPointMap().remove(resp.getResGroupId());
                            marketModule.userData.setUpdateOption();
                        }
                    }

                    marketModule.userData.setGuanYin(marketModule.userData.getGuanYin() - marketModule.resGrabNeedPhysical);
                    int logType = 4;
                    //体力变更
                    AutoLogMgr.add(new NewMonopolyMarketUserPhysicalChangeLogData(player.getUserId(), marketModule.userData.getWeekId(),
                            marketModule.userData.getOfficeCurPos(), marketModule.userData.getOfficeLevel(),marketModule.userData.getOfficeNo(),
                            logType, oldValue, marketModule.userData.getGuanYin()));
                }

                // 加掠夺奖励
                Property robReward = NewMonopolyMarketMgr.addReward(resp.getType(), resp.getRewardNum());
                player.getModule(CurrencyModule.class).addCurrency(robReward,eLogMoneyType.NewMonopolyMarket, eLogMoneyType.NewMonopolyMarketResGrabRobReward);

            }else {
                // 撤回
                NewMonopolyMarketProto.NewMonopolyMarketSynPointSettlementToGameServerMsg settlement = resp.getSettlement();
                Property reward = NewMonopolyMarketMgr.addReward(resp.getType(),resp.getRewardNum());
                player.getModule(CurrencyModule.class).addCurrency(reward,eLogMoneyType.NewMonopolyMarket, eLogMoneyType.NewMonopolyMarketResGrabRecallReward);

                NewMonopolyMarketProto.NewBroadcastMonopolyMarketRespMsg.Builder builder  = NewMonopolyMarketProto.NewBroadcastMonopolyMarketRespMsg.newBuilder();
                builder.setType(2);
                builder.addResGrabTip(NewMonopolyMarketMgr.builderMonopolyMarketResGrabTipEntity(settlement));
                player.sendPacket(YanQuMessageUtils.buildMessage(ClientProtocol.U_NEW_MONOPOLY_MARKET_BROADCAST, builder));

                NewMonopolyMarketMgr.leaveResGrabPoint(player, resp.getResGroupId(), settlement.getPosIndex(),false);
            }
            // 加入冲榜
            // MonopolyMarketRankActivityMgr.addRankValue(player.getUserId(), resp.getRewardNum());

            NewMonopolyMarketMgr.refreshUser(player,true,true);
        }
        player.sendPacket(ClientProtocol.U_NEW_MONOPOLY_MARKET_RES_GRAB_ACTION, resp.getStcMsg().toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
